const db = wx.cloud.database()
const _ = db.command
const cache = require('../utils/cache')
const message = db.collection('message')
const UserInfo = cache.getUserInfo()
// 消息中心
// msgType 是消息类别，比如上课提醒、实践任务提醒、系统信息
// Members 这是接收信息的成员数组 
// content 这是发布的提醒内容
const Message = (msgType = "", Members = [], content) => {
  Members.push(UserInfo)
  let newgetmember = [];
  Members.forEach((i) => {
    newgetmember.some((item) => item.account == i.account) ? '' : newgetmember.push(i);
  });
  switch (msgType) {
    case 'CourseMsg':
      message.add({
        data: {
          MsgMembers: newgetmember,
          msgType: 'CourseMsg',
          title: "你有一条新的课程信息",
          content: content,
          timeTs: Date.now(),
          permissions: true,
        },
        success: function (res) {
          console.log('课程提醒创建成功', res)
        }
      })
      break;
    case 'PracticeMsg':
      message.add({
        data: {
          MsgMembers: newgetmember,
          msgType: 'PracticeMsg',
          title: "你有一条新的实践信息",
          content: content,
          timeTs: Date.now(),
          permissions: true,
        },
        success: function (res) {
          console.log('实践提醒创建成功', res)
        }
      })
      break;
    case 'SystemMsg':
      message.add({
        data: {
          MsgMembers: newgetmember,
          msgType: 'SystemMsg',
          title: "你有一条新的系统信息",
          content: content,
          timeTs: Date.now(),
          permissions: true,
        },
        success: function (res) {
          console.log('创建成功', res)
        }
      })
      break;
    default:
      message.add({
        data: {
          MsgMembers: Members,
          msgType: msgType,
          content: content || "你有一条新的课程安排信息，请去我的课程查看详情。",
          timeTs: Date.now(),
          permissions: true,
        },
        success: function (res) {
          console.log('创建成功', res)
        }
      })
      break;
  }

}




module.exports = {
  Message
}